using System;
using System.Collections;
using System.Collections.Generic;
using GameManager.Event;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 教学
/// </summary>
public class UITutorise : MonoBehaviour
{
    [HideInInspector]
    public RectTransform rect;
    public Image LevelTrainImage;

    public MouseTrigger LevelTrainMouseTrigger;

    public GameObject jiantou;

    public Image VictoryUI;
    public Image FailUI;

    public Button VictoryButton;
    public Button FailButton;

    private int TrainCount;

    private void Awake()
    {
        rect = GetComponent<RectTransform>();
    }
    
    // Start is called before the first frame update
    void Start()
    {
        EventManager.Instance.Subscribe(LevelTown1CompleteEvent.EventId,LevelTown1CompleteCallBack);
        EventManager.Instance.Subscribe(CanLevelEvent.EventId,CanLevelCallBack);
        EventManager.Instance.Subscribe(DontLevelEvent.EventId,DontLevelCallBack);
        EventManager.Instance.Subscribe(LevelTrainCompleteEvent.EventId,LevelTrainCompleteCallBack);
        EventManager.Instance.Subscribe(VictoryEvent.EventId,VictoryEventCallBack);
        EventManager.Instance.Subscribe(FailEvent.EventId,FailEventCallBack);
        
        VictoryButton.onClick.AddListener(() =>
        {
            Luna.Unity.Playable.InstallFullGame();
        });
        FailButton.onClick.AddListener(() =>
        {
            Luna.Unity.Playable.InstallFullGame();
        });
    }

    private void OnDestroy()
    {
        EventManager.Instance.UnSubscribe(LevelTown1CompleteEvent.EventId,LevelTown1CompleteCallBack);
        EventManager.Instance.UnSubscribe(CanLevelEvent.EventId,CanLevelCallBack);
        EventManager.Instance.UnSubscribe(DontLevelEvent.EventId,DontLevelCallBack);
        EventManager.Instance.UnSubscribe(LevelTrainCompleteEvent.EventId,LevelTrainCompleteCallBack);
        EventManager.Instance.UnSubscribe(VictoryEvent.EventId,VictoryEventCallBack);
        EventManager.Instance.UnSubscribe(FailEvent.EventId,FailEventCallBack);
    }

    // Update is called once per frame
    void Update()
    {
        if (LevelTrainImage.gameObject.activeSelf)
        {
            LevelTrainImage.rectTransform.anchoredPosition =
                UpdateRectPosition(LevelTrainMouseTrigger.transform.position, new Vector2(0, 100));
        }
    }

    private Vector2 UpdateRectPosition(Vector3 EnemyTransform,Vector2 Offset)
    {
        Vector3 MyWorldPosition =EnemyTransform;
        Vector2 screenPosition = Camera.main.WorldToScreenPoint(MyWorldPosition);
        Vector2 localPosition;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rect,screenPosition,null,out localPosition))
        {
            return localPosition + Offset;
        }
        return Vector2.zero;
    }

    private void LevelTown1CompleteCallBack(object sender,EventArgs ea)
    {
        LevelTrainImage.gameObject.SetActive(true);
    }

    private void CanLevelCallBack(object sender,EventArgs ea)
    {
        jiantou.gameObject.SetActive(true);
    }

    private void DontLevelCallBack(object sender,EventArgs ea)
    {
        jiantou.gameObject.SetActive(false);
    }

    private void LevelTrainCompleteCallBack(object sender,EventArgs ea)
    {
        TrainCount += 1;
        if (TrainCount == 2)
        {
            LevelTrainImage.gameObject.SetActive(false);
            GameEntry.Instance.mouseTrigger4.gameObject.SetActive(false);
        }
    }

    private void VictoryEventCallBack(object sender,EventArgs ea)
    {
        VictoryUI.gameObject.SetActive(true);
    }

    private void FailEventCallBack(object sender,EventArgs ea)
    {
        FailUI.gameObject.SetActive(true);
    }
    
    
}
